package com.wanwen.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanwen.dto.ModuleDto;
import com.wanwen.entity.Modules;
import com.wanwen.entity.RoleModules;
import com.wanwen.mapper.ModuleMapper;
import com.wanwen.mapper.ModuleRoleMapper;
import com.wanwen.utils.CommonUtil;
import com.wanwen.utils.Result;
import com.wanwen.utils.UITreeFactory;

@RestController
@RequestMapping("/moduleRole")
public class ModuleRoleController {
	@Autowired
	private ModuleMapper moduleMapper;
	@Autowired
	private ModuleRoleMapper moduleRoleMapper;

	/**
	 * 查询所有模块
	 * 
	 * @return layui加载树形组件需要的json数据
	 */
	@RequestMapping("/getAllByRoleId")
	public Object getAllByRoleId(Integer roleId) {
		// 获取所有的模块
		List<Modules> allModules = moduleMapper.selectList(new QueryWrapper<Modules>().orderByAsc("weight"));
		// 所有的已转的模块业务对象
		List<ModuleDto> mdtoList = new ArrayList<ModuleDto>();
		for (Modules all : allModules) {
			mdtoList.add(CommonUtil.getModuleDtoByModule(all));
		}
		// 根据角色id查询角色模块表
		List<RoleModules> roleModules = moduleRoleMapper
				.selectList(new QueryWrapper<RoleModules>().eq("role_id", roleId));
		// 根据多个角色Id查询模块
		if (roleModules.size() == 0) {
			// moduleByRole = moduleMapper.selectList(new QueryWrapper<Modules>().in("id",
			// modulIds).orderByAsc("weight"));
			UITreeFactory.loadTree(mdtoList);
			return UITreeFactory.moduleTreeList;
		} else {
			// 初始化集合用来存储用户角色表查询出的角色Id
			List<Integer> modulIds = new ArrayList<Integer>();
			// 存储角色Id
			for (RoleModules item : roleModules) {
				modulIds.add(item.getModulId());
			}
			List<Modules> moduleByRole = moduleMapper
					.selectList(new QueryWrapper<Modules>().in("id", modulIds).orderByAsc("weight"));
			for (Modules byRole : moduleByRole) {
				for (ModuleDto dto : mdtoList) {
					if (byRole.getId() == dto.getId() && dto.getParentId() != 0) {
						dto.setChecked(true);// 回显
					}
				}
			}
			UITreeFactory.loadTree(mdtoList);
			return UITreeFactory.moduleTreeList;
		}
	}

	/**
	 * @param mdIds 模块id通过逗号连接的字符串
	 * @param roleId 角色id
	 * @return
	 */
	@RequestMapping("/updateRoleMpdules")
	public Object updateRoleMpdules(String mdIds,Integer roleId) {
		//先根据roleId把角色模块表中 下所有的数据全部删除
		QueryWrapper<RoleModules> wrapper=new QueryWrapper<RoleModules>();
		wrapper.eq("role_id", roleId);
		moduleRoleMapper.delete(wrapper);
		//从模块id拼接的字符串中解析出每个模块Id,然后创建相关的对象
		String[] ids=mdIds.split(",");
		int rs = 0;
		for(String s:ids) {
			RoleModules rm=new RoleModules();
			rm.setRoleId(roleId);
			rm.setModulId(Integer.valueOf(s));
			rs = moduleRoleMapper.insert(rm);
		}
		Result res = new Result(rs==1?"成功":"失败", "", rs==1?true:false);
		 return res;
	}
	
}
